﻿using Microsoft.Extensions.Configuration;
using MiniFox.Configurations;
using MiniFox.Web;
using System;
using System.Configuration;
using System.Web;

namespace MiniFox.Platform.Applications.Configurations
{
    /// <summary>
    /// 
    /// </summary>
    [Section(ELEMENT_NAME)]
    public sealed class UserAuthClientConfiguration
    {
        internal const string ELEMENT_NAME = "minifox:userAuthClient";

        const string AUTHENTICATE_URL = "/authorization/connect";
        const string SIGNOUT_URL = "/authorization/signout";
        const string IDENTITY_URL = "/accesstoken/identity";
        const string MODULE_ROLES_URL = "/platform/roles";
        const string ISSUE_TOKEN_URL = "/accesstoken/issue";
        const string RENEW_TOKEN_URL = "/accesstoken/renew";
        const string AGENT_CONNECT_URL = "/oauthagent/connect";

        const int RENEW_INTERVAL = 3600;//秒
        const int COOKIE_STATE_EXPIRES = 36000;

        /// <summary>
        /// 应用程序ID
        /// </summary>
        [ConfigurationKeyName("appId")]
        public string ApplicationID
        {
            get;
            set;
        }

        /// <summary>
        /// 应用程序管理员
        /// </summary>
        [ConfigurationKeyName("sysAdmin")]
        public string SysAdmin
        {
            get;
            set;
        }
        /// <summary>
        /// 应用程序安全访问码
        /// </summary>
        [ConfigurationKeyName("secretKey")]
        public string SecretKey
        {
            get;
            set;
        }
        /// <summary>
        /// SSO验证主机，必须设置为应用程序虚拟路径(例如: http://www.xxxxxx.com:8888 ,  http://www.xxxxxx.com:8080/passport , http://passport.xxxxxx.com)
        /// </summary>
        [ConfigurationKeyName("host")]
        public string Host
        {
            get;
            set;
        }
        /// <summary>
        /// 验证地址
        /// </summary>
        [ConfigurationKeyName("authUrl")]
        public string AuthUrl
        {
            get;
            set;
        } = AUTHENTICATE_URL;
        /// <summary>
        /// 
        /// </summary>
        [ConfigurationKeyName("signOutUrl")]
        public string SignOutUrl
        {
            get;
            set;
        } = SIGNOUT_URL;
        /// <summary>
        /// 获取身份信息服务地址
        /// </summary>
        [ConfigurationKeyName("identityUrl")]
        public string IdentityUrl
        {
            get;
            set;
        } = IDENTITY_URL;
        /// <summary>
        /// 获取模块角色清单
        /// </summary>
        [ConfigurationKeyName("moduleRolesUrl")]
        public string ModuleRolesUrl
        {
            get;
            set;
        } = MODULE_ROLES_URL;
        /// <summary>
        /// 
        /// </summary>
        [ConfigurationKeyName("issueTokenUrl")]
        public string IssueTokenUrl
        {
            get;
            set;
        } = ISSUE_TOKEN_URL;
        /// <summary>
        /// 令牌刷新地址
        /// </summary>
        [ConfigurationKeyName("renewTokenUrl")]
        public string RenewTokenUrl
        {
            get;
            set;
        } = RENEW_TOKEN_URL;
        /// <summary>
        /// 验证后，默认跳转地址
        /// </summary>
        [ConfigurationKeyName("defaultUrl")]
        public string DefaultUrl
        {
            get;
            set;
        }
        /// <summary>
        /// 代理回接地址
        /// </summary>
        [ConfigurationKeyName("agentConnectUrl")]
        public string AgentConnectUrl
        {
            get;
            set;
        } = AGENT_CONNECT_URL;
        /// <summary>
        /// 刷新identity间隔时长(秒),默认值3600秒(1小时)
        /// </summary>
        [ConfigurationKeyName("renewInterval")]
        public int RenewInterval
        {
            get;
            set;
        } = RENEW_INTERVAL;
        /// <summary>
        /// cookie状态过期时长(秒),默认值36000秒(10小时)
        /// </summary>
        [ConfigurationKeyName("cookieStateExpires")]
        public int CookieStateExpires
        {
            get;
            set;
        } = COOKIE_STATE_EXPIRES;
        
        /// <summary>
        /// 获取完整的验证地址url, Host + AuthUrl (例如： http://passport.xxxxxx.xxx/Secure/Authenticate)
        /// </summary>
        public string FullAuthUrl
        {
            get
            {
                string url = this.AuthUrl;
                if (url.StartsWith(WebUtil.HTTPS) || url.StartsWith( WebUtil.HTTP))
                    return url;
                url = WebUtil.BuildFullUrl(this.Host, url);
                return url;
            }
        }
        /// <summary>
        /// 获取完整的身份查询地址, Host + IdentityUrl (例如： http://passport.xxxxxx.xxx/accesstoken/identity)
        /// </summary>
        public string FullIdentityUrl
        {
            get
            {
                string url = this.IdentityUrl;
                if (url.StartsWith(WebUtil.HTTPS) || url.StartsWith(WebUtil.HTTP))
                    return url;
                url = WebUtil.BuildFullUrl(this.Host, url);
                return url;
            }
        }
        /// <summary>
        /// 获取完整的身份查询地址, Host + IdentityUrl (例如： http://platform.xxxxxx.xxx/platform/roles)
        /// </summary>
        public string FullModuleRolesUrl
        {
            get
            {
                string url = this.ModuleRolesUrl;
                if (url.StartsWith(WebUtil.HTTPS) || url.StartsWith(WebUtil.HTTP))
                    return url;
                url = WebUtil.BuildFullUrl(this.Host, url);
                return url;
            }
        }
        /// <summary>
        /// 获取完整的用户注销地址, Host + SignOutUrl (例如： http://passport.xxxxxx.xxx/Secure/signout)
        /// </summary>
        public string FullSignOutUrl
        {
            get
            {
                string url = this.SignOutUrl;
                if (url.StartsWith(WebUtil.HTTPS) || url.StartsWith(WebUtil.HTTP))
                    return url;
                url = WebUtil.BuildFullUrl(this.Host, url);
                return url;
            }
        }
        /// <summary>
        /// 获取完整的access token地址url, Host + CreateTokenUrl (例如： http://passport.xxxxxx.xxx/Secure/CreateAccessToken)
        /// </summary>
        public string FullIssueTokenUrl
        {
            get
            {
                string url = this.IssueTokenUrl;
                if (url.StartsWith(WebUtil.HTTPS) || url.StartsWith(WebUtil.HTTP))
                    return url;
                url = WebUtil.BuildFullUrl(this.Host, url);
                return url;
            }
        }
        /// <summary>
        /// 获取完整的刷新token地址, Host + RenewTokenUrl (例如： http://passport.xxxxxx.xxx/secure/RenewAccessToken)
        /// </summary>
        public string FullRenewTokenUrl
        {
            get
            {
                string url = this.RenewTokenUrl;
                if (url.StartsWith(WebUtil.HTTPS) || url.StartsWith(WebUtil.HTTP))
                    return url;
                url = WebUtil.BuildFullUrl(this.Host, url);
                return url;
            }
        }
        ///// <summary>
        ///// 获取完整的代理回接地址, Host + AgentConnectUrl (例如： http://passport.xxxxxx.xxx/secure/RenewAccessToken)
        ///// </summary>
        //public string FullAgentConnectUrl
        //{
        //    get
        //    {
        //        string url = this.AgentConnectUrl;

        //        url = WebUtil.BuildFullUrl(this.Host, url);
        //        return url;
        //    }
        //}
        /// <summary>
        /// 获取例外地址集，该地址集中的URL不需要登录即可访问
        /// </summary>
        [ConfigurationKeyName("exceptionUrls")]
        public string[] ExceptionUrls
        {
            get;
            set;
        }
    }
}
